package org.g4studio.demo.online.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.g4studio.common.util.WebUtils;
import org.g4studio.common.web.BaseAction;
import org.g4studio.common.web.BaseActionForm;
import org.g4studio.core.json.JsonHelper;
import org.g4studio.core.metatype.Dto;
import org.g4studio.core.metatype.impl.BaseDto;
import org.g4studio.core.mvc.xstruts.action.ActionForm;
import org.g4studio.core.mvc.xstruts.action.ActionForward;
import org.g4studio.core.mvc.xstruts.action.ActionMapping;
import org.g4studio.core.web.report.fcf.Categorie;
import org.g4studio.core.web.report.fcf.CategoriesConfig;
import org.g4studio.core.web.report.fcf.DataSet;
import org.g4studio.core.web.report.fcf.FcfDataMapper;
import org.g4studio.core.web.report.fcf.GraphConfig;
import org.g4studio.core.web.report.fcf.Set;
import org.g4studio.demo.online.service.DemoService;
import org.g4studio.system.common.dao.vo.UserInfoVo;
import org.g4studio.common.util.ParamUtil;

/**
 * 演示用 Action
 * 
 * @author XiongChun
 * @since 2010-06-13
 * @see BaseAction
 */
public class TestAction extends BaseAction {

	private String encoding = "UTF-8";
	private String contentType = "application/json";

	private DemoService demoService = (DemoService)getService("demoService");
	

	/**
	 * 测试页面1初始化
	 * 
	 * @param
	 * @return
	 */
	public ActionForward test1Init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("test1View");
	}
	

	/**
	 * 测试页面2初始化
	 * 
	 * @param
	 * @return
	 */
	public ActionForward test2Init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.setAttribute("ID", "测试");
		return mapping.findForward("test2View");
	}

	/**
	 * 测试3页面初始化
	 * 
	 * @param
	 * @return
	 */
	public ActionForward test3Init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("test3View");
	}

	/**
	 * 测试4页面初始化
	 * 
	 * @param
	 * @return
	 */
	public ActionForward test4Init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("test4View");
	}

	/**
	 * 测试5页面初始化
	 * 
	 * @param
	 * @return
	 */
	public ActionForward test5Init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
        
		return mapping.findForward("test5View");
	}

	/**
	 * htable
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward htable(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								   HttpServletResponse response) throws Exception {

		return mapping.findForward("htable");
	}

	/**
	 * 下一步 wizard
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward wizard(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								HttpServletResponse response) throws Exception {

		return mapping.findForward("wizard");
	}

	/**
	 * jqgrid
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward jqgrid(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								HttpServletResponse response) throws Exception {

		return mapping.findForward("jqgrid");
	}
	
	/**
	 * 获取FlashReport元数据 (折线组合图)
	 * @param pDto
	 * @return
	 */
	private List getFcfDataList4LineGroup(Dto pDto){
		pDto.put("rownum", "12");
		List dataList = new ArrayList();
		DataSet dataSet1 = new DataSet();
		dataSet1.setSeriesname("产品A");
		dataSet1.setColor("FDC12E");
		pDto.put("product", "1");
		List alist = g4Reader.queryForList("Demo.getFcfDataList", pDto);
		List aSetList = new ArrayList();
		for (int i = 0; i < alist.size(); i++) {
			Dto dto = (BaseDto)alist.get(i);
			Set set = new Set();
			set.setValue(dto.getAsString("value"));
			aSetList.add(set);
		}
		dataSet1.setData(aSetList);
		dataList.add(dataSet1);
		
		DataSet dataSet2 = new DataSet();
		dataSet2.setSeriesname("产品B");
		dataSet2.setColor("44BC2F");
		pDto.put("product", "2");
		List blist = g4Reader.queryForList("Demo.getFcfDataList", pDto);
		List bSetList = new ArrayList();
		for (int i = 0; i < blist.size(); i++) {
			Dto dto = (BaseDto)blist.get(i);
			Set set = new Set();
			set.setValue(dto.getAsString("value"));
			bSetList.add(set);
		}
		dataSet2.setData(bSetList);
		dataList.add(dataSet2);
		return dataList;
	}
	
	public ActionForward saveTheSubmitInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BaseActionForm aForm = (BaseActionForm)form;
		Dto dto = aForm.getParamAsDto(request);
		System.out.println("接收到的表单提交参数：\n" + dto);
		response.setCharacterEncoding("UTF-8");
		//int i = 3/0;
		demoService.doError();
		setOkTipMsg("数据提交成功:" + dto.toString(), response);
		return mapping.findForward(null);
	}
	
	public ActionForward loadCallBack(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Dto outDto = new BaseDto();
		outDto.put("text1", "熊春");
		outDto.put("text2", "托尼贾");
		String jsonString = JsonHelper.encodeDto2FormLoadJson(outDto, null);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}

	public ActionForward jqgridJsonDemo(ActionMapping mapping, ActionForm form, HttpServletRequest request,
										HttpServletResponse response) throws Exception {
		this.contentType = contentType + ";charset=" + encoding;
		int lstSize = 10;
		Dto dto = new BaseDto();
		dto.put("id",2);
//		System.out.println("g4Reader 查询"+g4Reader.queryForList("cw_enterprise.CwEnterprise_selectByPrimaryKey", dto));

		StringBuilder strjson = new StringBuilder();

		strjson.append("{");
		strjson.append("\"totalpages\" : ");
		strjson.append("\"").append(1).append("\",");

		strjson.append("\"currpage\" : ");
		strjson.append("\"").append(1).append("\",");
		strjson.append("\"totalrecords\" : ");
		strjson.append("\"").append(10).append("\",");

		strjson.append("\"dataroot\" : [");

		for (int i = 0; i < lstSize; i++) {
			strjson.append("{\"no\":");
			strjson.append("\"").append(i);
			strjson.append("\",");
			strjson.append("\"busynessName\":").append("\"")
					.append(i);
			strjson.append("\"}");
			if (i != lstSize - 1) {
				strjson.append(",");
			}
		}
		strjson.append("]}");
		response.setContentType(contentType);
		response.setCharacterEncoding(encoding);
		PrintWriter pw = response.getWriter();
		pw.write(strjson.toString());
		pw.flush();
		return null;
	}

	/**
	 * 添加报功任务
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addWTRTask(ActionMapping mapping, ActionForm form, HttpServletRequest request,
							 HttpServletResponse response) throws Exception {

		return mapping.findForward("addWTRTask");
	}

	public ActionForward listwtr(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								 HttpServletResponse response) throws Exception {
		return mapping.findForward("listwtr");
	}

	/**
	 * 任务列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward listwtrjson(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								 HttpServletResponse response) throws Exception {
		BaseActionForm aForm = (BaseActionForm)form;
		Dto dto = aForm.getParamAsDto(request);
		UserInfoVo userInfo = WebUtils.getSessionContainer(request).getUserInfo();
		dto.put("userid",userInfo.getUserid());
		List list = g4Reader.queryForPage("Demo.listWTRTask", dto);
		System.out.println("列表大小="+list.size());
		Integer countInteger = (Integer) g4Reader.queryForObject("Demo.countFiles", dto);
		countInteger = list.size();
		String jsonString = JsonHelper.encodeList2PageJsonJqgrid(list, countInteger, null);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}

	public ActionForward completeTask(ActionMapping mapping, ActionForm form, HttpServletRequest request,
									 HttpServletResponse response) throws Exception {

		return mapping.findForward("completeTask");
	}

	public ActionForward saveWTRTask(ActionMapping mapping, ActionForm form, HttpServletRequest request,
									 HttpServletResponse response) throws Exception {
		System.out.println("saveWTR方法");
		UserInfoVo userInfo = WebUtils.getSessionContainer(request).getUserInfo();
		Map<String, Object> params = ParamUtil.paramMap(request);
		log.info("params"+request.getParameter("value")+params.get("priorityRadio"));
		System.out.println("saveWTR方法 params="+params);
		Map map = request.getParameterMap();
		Iterator keyIterator = (Iterator) map.keySet().iterator();
		Dto pDto=new BaseDto();
		while (keyIterator.hasNext()) {
			String key = (String) keyIterator.next();
			String value = ((String[]) (map.get(key)))[0];
			pDto.put(key, value);
		}
		pDto.put("createuserid",userInfo.getUserid());
		System.out.println("saveWTRTask方法 userid="+userInfo.getUserid());
		demoService.insertWTRTask(pDto);
//		return mapping.findForward("test1View");
		return mapping.findForward(null);
	}

	public ActionForward updateWTRTask(ActionMapping mapping, ActionForm form, HttpServletRequest request,
									   HttpServletResponse response) throws Exception {
//		CommonActionForm cForm = (CommonActionForm) form; 之前版本的取法
		BaseActionForm cForm = (BaseActionForm) form;
		Dto inDto = cForm.getParamAsDto(request);
		demoService.updateWTRTask(inDto);
		setOkTipMsg("数据修改成功", response);
		return mapping.findForward(null);
	}
}
